from django.db import connection
from django.http import HttpResponse
from django.shortcuts import render

from database_demo.models import Book


# Create your views here.


def index(request):
    #获取游标对象
    # cursor = connection.cursor()
    # #拿到游标对象后执行sql语句
    # cursor.execute("select * from book")
    # rows = cursor.fetchall()
    # for row in rows:
    #     print(row)
    # return HttpResponse("查找成功")
    pass

def add_book(request):
    book = Book(name="三国演义",author="罗贯中",price=10)
    book.save()
    return HttpResponse("图书插入成功")

def query_book(request):
    # books = Book.objects.all()

    # 过滤
    # books = Book.objects.filter(name="三国演义")
    # for book in books:
    #     print(book.name)

    # 获取第一个满足条件
    # books = Book.objects.get(name="三国演义")
    # print(books.name)

    # 数据排序，如果逆序Book.objects.order_by("-pub_time")
    # books = Book.objects.order_by("-pub_time")
    # for book in books:
    #     print(book.name)

    # 修改数据
    # books = Book.objects.get(name="三国演义")
    # books.name = "西游记"
    # books.save()

    # 删除数据
    books = Book.objects.get(name="西游记")
    books.delete()
    return HttpResponse("删除成功")
